package com.drinks.yiniang.ui.activity.user.points;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.user.points.CouponAdapter;
import com.drinks.yiniang.common.recycler.decoration.ItemSpacesDecoration;
import com.drinks.yiniang.entity.user.points.CouponJson;
import com.drinks.yiniang.io.api.CouponApiIO;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 选择优惠券页面
 *
 * @author han
 * @date 2019/09/08
 */
public class CouponActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    //nav
    private NavigationBarLayout nav_bar_coupon;
    //列表
    private BaseRecyclerView rcv_coupon;

    private CouponAdapter mCouponAdapter;

    //优惠券类型
    private String mCouponType;

    @Override
    protected void initView() {
        nav_bar_coupon = findViewById(R.id.nav_bar_coupon);

        rcv_coupon = findViewById(R.id.rcv_coupon);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        if(getIntent() != null) {
            mCouponType = getIntent().getStringExtra(Constants.KeyType);
        }

        if(Constants.TYPE_COUPON_SHOP.equals(mCouponType)) {//门店券
            nav_bar_coupon.setNavBarTitle(R.string.text_shop_coupon_simple);
        } else {//通用券
            nav_bar_coupon.setNavBarTitle(R.string.text_coupon);
        }

        initAdapter();
        getCouponList();
    }

    private void initAdapter() {
        rcv_coupon.setLayoutManager(new LinearLayoutManager(mContext));
        rcv_coupon.addItemDecoration(ItemSpacesDecoration.getLRTItemCountDPDecoration(10));

        mCouponAdapter = new CouponAdapter(mContext, new ArrayList<CouponJson.CouponList>());
        mCouponAdapter.setCenterEmpty(getLayoutInflater(), rcv_coupon);
        mCouponAdapter.bindToRecyclerView(rcv_coupon);
    }

    @Override
    protected void initListener() {
        nav_bar_coupon.setOnNavgationBarClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_coupon;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    //获取优惠券列表
    private void getCouponList() {
        if(StringUtils.isEmpty(mCouponType)) {
            ToastUtils.showShort("优惠券类型异常");
            return;
        }

        showLoadingDialog();

        CouponApiIO.getInstance().getCouponList(mCouponType, new APIRequestCallback<CouponJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(CouponJson res) {
                if(mCouponAdapter == null) {
                    return;
                }

                mCouponAdapter.getData().clear();
                mCouponAdapter.addData(res.getData());
                mCouponAdapter.loadMoreEnd();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mCouponAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
